# replication-code.r
# This file generates the results in Rice, Rhodes, and Nteta,
# "Same As It Ever Was? The Impact of Racial Resentment on White Juror Decision-Making", 
# The Journal of Politics
#
# dr 2.18.2021

library(haven)
library(ggplot2)
library(gridExtra)
library(stm)
library(readstata13)
library(ggeffects)


## ===================================================================== ####
## ====================  Read in and set up data ======================= ####
## ===================================================================== ####

myData <- read.dta13("replication-data.dta")

# update data so it doesn't treat prosecutor / defendant flip in order as a separate treatment
myData$drVignette <- 0
myData$drVignette[which(is.na(myData$Vignette_treat))] <- NA
myData$drVignette[myData$Vignette_treat == "Version B"|myData$Vignette_treat == "Version D"] <- 1

# add party variable
myData$party <- 0
myData$party[grep("Democrat", myData$pid7)] <- -1
myData$party[grep("Republican", myData$pid7)] <- 1

## ============================================================ ####
## ============  Appendix Figures 1 & 2: Difference in Means ==================== ####
## =========================================================== ####

# recode guilty
myData$guilty <- NA
myData$guilty[which(myData$UMA404 == "Not guilty")] <- 0
myData$guilty[which(myData$UMA404 == "Guilty")] <- 1

# recode length
myData$length <- myData$UMA405/60

# check on who respondents think is guilty; 1 is guilty, 0 not guilty
t.test(myData$guilty ~ myData$drVignette)
 
# check on sentence length
t.test(myData$length ~ myData$drVignette)

# create data for plot
plotGuilt <- as.data.frame(aggregate(myData$guilty, by=list(myData$drVignette), FUN=mean, na.rm=T))
plotGuilt$se <- c(sd(myData$guilty[myData$drVignette==0], na.rm=T)/(sqrt(length(which(myData$drVignette==0)))),
					sd(myData$guilty[myData$drVignette==1], na.rm=T)/(sqrt(length(which(myData$drVignette==1)))))
colnames(plotGuilt) <- c("Treatment", "Mean", "SE")					
plotSent <- as.data.frame(aggregate(myData$length, by=list(myData$drVignette), FUN=mean, na.rm=T))
plotSent$se <- c(sd(myData$length[myData$drVignette==0], na.rm=T)/(sqrt(length(which(myData$drVignette==0)))),
					sd(myData$length[myData$drVignette==1], na.rm=T)/(sqrt(length(which(myData$drVignette==1)))))
colnames(plotSent) <- c("Treatment", "Mean", "SE")

# plot guilt
p1 <- ggplot(plotGuilt) + 
	geom_point(aes(plotGuilt$Treatment, plotGuilt$Mean), size=3) +
	geom_segment(aes(x=plotGuilt$Treatment, y=plotGuilt$Mean + plotGuilt$SE * 1.96, xend=plotGuilt$Treatment, yend=plotGuilt$Mean - plotGuilt$SE * 1.96), size=1.25) +  
	scale_y_continuous(name=c(""), breaks=c(0,1), labels=c("Not Guilty", "Guilty"), limits=c(0,1)) +
	scale_x_continuous(name=c("Race Treatment"), breaks=c(0,1), labels=c("White","Black"), limits=c(-0.25,1.25)) +
	ggtitle("Verdict") +
	theme_bw()

# plot sentence
p2 <- ggplot(plotSent) + 
	geom_point(aes(plotSent$Treatment, plotSent$Mean), size=3) +
	geom_segment(aes(x=plotSent$Treatment, y=plotSent$Mean + plotSent$SE * 1.96, xend=plotSent$Treatment, yend=plotSent$Mean - plotSent$SE * 1.96), size=1.25) +  
	ylab("Months Sentenced / Maximum Sentence ") +
	ylim(0,.25) + 
	scale_x_continuous(name=c("Race Treatment"), breaks=c(0,1), labels=c("White","Black"), limits=c(-0.25,1.25)) +
	ggtitle("Sentence") +
	theme_bw()
	

# =-=-=-=-=-=-=-=-=-=-=-
# Left Panel, Figure 1: Plot of Racial Resentment
# =-=-=-=-=-=-=-=-=-=-=-

myData$Vote <- NA
myData$Vote[which(myData$guilty == 0)] <- "Not Guilty"
myData$Vote[which(myData$guilty == 1)] <- "Guilty"
rrData <- myData[-which(is.na(myData$Vote)),]
ggplot(rrData, aes(racialresentment)) + 
   geom_histogram(aes(fill=Vote), binwidth = .1) +
   scale_fill_manual(values = c("grey31", "grey66")) +
   xlab("Racial Resentment") +
   theme_bw()

# =-=-=-=-=-=-=-=-=-=-=-
# Main models
# =-=-=-=-=-=-=-=-=-=-=-

# Appendix Table 1
myData$vignetteFactor <- as.factor(myData$drVignette)
logitBasic <- glm(guilty ~ vignetteFactor, data=myData, family=binomial(link="logit"))
logitInt <- glm(guilty ~ vignetteFactor * racialresentment, data=myData, family=binomial(link="logit"))
# Appendix Table 3
regBasic <- lm(length ~ vignetteFactor, data=myData)
regInt <- lm(length ~ vignetteFactor * racialresentment, data=myData)

# Right panel, Figure 1: plot predicted probability from logit 
plotLogit <- ggpredict(logitInt, terms = c("racialresentment", "vignetteFactor"))
p1 <- plot(plotLogit, colors = c("grey76", "grey32"), alpha = .5)
p1 <- p1 + labs(x="Racial Resentment", y = "Predicted Probability of Guilty", title = "Predicted Values of Guilty") +
	  theme(legend.position="none", legend.title=element_blank()) +
	  scale_fill_manual(breaks = c(0,1), values=c("grey76", "grey32"), name="", labels=c("White", "Black")) + 
	  scale_color_manual(breaks = c(0,1), values=c("grey76", "grey32"), name="", labels=c("White", "Black")) 
	  
# Appendix Figure 3: plot predicted values of linear regression
plotReg <- ggpredict(regInt, terms = c("racialresentment", "vignetteFactor"))
p2 <- plot(plotReg, colors = c("grey66", "grey32"), alpha=0.3)
p2 <- p2 + labs(x="Racial Resentment", y = "Sentence Length / Maximum Sentence", title = "Predicted Values of Sentence") +
	  theme(legend.position = "none") +
	  scale_fill_manual(breaks = c(0,1), values=c("grey55", "grey4"), name="", labels=c("White", "Black")) + 
	  scale_color_manual(breaks = c(0,1), values=c("grey66", "grey32"), name="", labels=c("White", "Black"))
	  
## =-=-=-=-=-=-=-=-=-==-
##  Interaction tests
## =--=-=-=-=-=-=-=-=-=-

library(interflex)
		
# Appendix Figure 4
flexGuilty <- interflex(Y = "guilty", X = "racialresentment", D = "drVignette", treat.type = "discrete", estimator = "kernel", Xlabel = "Racial Resentment", Ylabel = "Guilty", Dlabel = "Treatment", main = "Dependent Variable = Vote for Guilt", nbins=5, data=myData, na.rm=T, theme.bw = T)

# Appendix Figure 5
flexSent <- interflex(Y = "length", X = "racialresentment", D = "drVignette", treat.type = "discrete", estimator = "kernel", Xlabel = "Racial Resentment", Dlabel = "Treatment", Ylabel = "Length", main = "Dependent Variable = Sentence Length", nbins=5, data=myData, na.rm=T, theme.bw = T)

## ===================================================================== ####
## ============  Structural topic model with treatment ================= ####
## ===================================================================== ####

# get data ready
processed <- textProcessor(myData$UMA406, stem=F, metadata = myData, sparselevel=0.998, customstopwords = c("bradley", "schwartz", "jamal", "gaines"))
out <- prepDocuments(processed$documents, processed$vocab, processed$meta)
docs <- out$documents
vocab <- out$vocab
meta <- out$meta

# fit basic model
stmFit <- stm(documents = out$documents,
			  vocab = out$vocab, 
			  K = 6, 
			  prevalence =~ drVignette * racialresentment, 
			  max.em.its = 1000, seed=34680349,
			  data = out$meta, 
			  init.type = "Spectral")


#select model to work with
selected6ModelResentment <- stmFit

#Figure out what topics are
labelTopics(selected6ModelResentment, c(1,2,3,4,5,6))

thoughts1 <- findThoughts(selected6ModelResentment, texts=out$meta$UMA406, n=10, topics=1)$docs[[1]]

thoughts2 <- findThoughts(selected6ModelResentment, texts=out$meta$UMA406, n=10, topics=2)$docs[[1]]

thoughts3 <- findThoughts(selected6ModelResentment, texts=out$meta$UMA406, n=10, topics=3)$docs[[1]]

thoughts4 <- findThoughts(selected6ModelResentment, texts=out$meta$UMA406, n=10, topics=4)$docs[[1]]

thoughts5 <- findThoughts(selected6ModelResentment, texts=out$meta$UMA406, n=10, topics=5)$docs[[1]]

thoughts6 <- findThoughts(selected6ModelResentment, texts=out$meta$UMA406, n=10, topics=6)$docs[[1]]

			  
myLabels <- sageLabels(selected6ModelResentment)$marginal
myLabels <- myLabels$frex[,1:5]
myTopics <- apply(myLabels, 1, paste, collapse=", ")
			  
# create effects plots
prep <- estimateEffect(c(6) ~ drVignette * racialresentment, selected6ModelResentment, metadata = out$meta, uncertainty = "None")
plot(prep, covariate = "racialresentment", moderator = "drVignette", model = selected6ModelResentment, 
	method = "continuous", moderator.value = 1, linecol="black", printlegend=F, ylim=c(0,.40))
plot(prep, covariate = "racialresentment", moderator = "drVignette", model = selected6ModelResentment, 
	method = "continuous", moderator.value = 0, linecol="darkgrey", add=T, printlegend=F, ylim = c(0,.40))

## ============================================================================ ####
## ============  Structural topic model with treatment & vote ================= ####
## ============================================================================ ####


processed <- textProcessor(myData$UMA406, stem=F, metadata = myData, sparselevel=0.998, customstopwords = c("bradley", "schwartz", "jamal", "gaines"))
out <- prepDocuments(processed$documents, processed$vocab, processed$meta)
docs <- out$documents
vocab <- out$vocab
meta <- out$meta

# fit stm
stmFit <- stm(documents = out$documents,
                      vocab = out$vocab, 
                      K = 6, 
                      prevalence =~ drVignette * racialresentment * guilty, 
                      max.em.its = 1000, seed=34680349,
                      data = out$meta, 
                      init.type = "Spectral")

selected6ModelResentment <- stmFit

#Figure out what topics are
labelTopics(selected6ModelResentment, c(1,2,3,4,5,6))

thoughts1 <- findThoughts(selected6ModelResentment, texts=out$meta$UMA406, n=10, topics=1)$docs[[1]]

thoughts2 <- findThoughts(selected6ModelResentment, texts=out$meta$UMA406, n=10, topics=2)$docs[[1]]

thoughts3 <- findThoughts(selected6ModelResentment, texts=out$meta$UMA406, n=10, topics=3)$docs[[1]]

thoughts4 <- findThoughts(selected6ModelResentment, texts=out$meta$UMA406, n=10, topics=4)$docs[[1]]

thoughts5 <- findThoughts(selected6ModelResentment, texts=out$meta$UMA406, n=10, topics=5)$docs[[1]]

thoughts6 <- findThoughts(selected6ModelResentment, texts=out$meta$UMA406, n=10, topics=6)$docs[[1]]


myLabels <- sageLabels(selected6ModelResentment)$marginal
myLabels <- myLabels$frex[,1:5]
myTopics <- apply(myLabels, 1, paste, collapse=", ")

# plots
plotData <- make.dt(selected6ModelResentment, meta=out$meta)
m1 <- lm(Topic1 ~ drVignette * racialresentment * guilty, data=plotData)
m2 <- lm(Topic2 ~ drVignette * racialresentment * guilty, data=plotData)
m3 <- lm(Topic3 ~ drVignette * racialresentment * guilty, data=plotData)
m4 <- lm(Topic4 ~ drVignette * racialresentment * guilty, data=plotData)
m5 <- lm(Topic5 ~ drVignette * racialresentment * guilty, data=plotData)
m6 <- lm(Topic6 ~ drVignette * racialresentment * guilty, data=plotData)

# Figure 2: Marginal Effects plots
library(margins)
plotData <- as.data.frame(plotData)

# topic 1
dat1 <- cplot(m1, x = "racialresentment", dx = "drVignette", what = "effect", data = plotData[plotData[["guilty"]] == 0,], draw = F)
dat2 <- cplot(m1, x = "racialresentment", dx = "drVignette", what = "effect", data = plotData[plotData[["guilty"]] == 1,], draw = F)
dat1$guilty <- "Not Guilty"
dat2$guilty <- "Guilty"
dat <- rbind(dat1, dat2)
ggplot(dat, aes(x = xvals, y=yvals)) + 
  geom_line(aes(color=guilty)) +
  geom_ribbon(aes(ymin = lower, ymax = upper, fill=guilty), alpha = 0.2) +
  geom_hline(yintercept = 0, linetype = 2) +
  ggtitle(myTopics[1]) +
  xlab("Racial Resentment") + ylab("Marginal Effect of Jamal Treatment") +
  scale_fill_manual(values = c("grey8", "grey47")) +
  scale_color_manual(values = c("darkgrey", "darkgrey")) + 
  theme_bw() + 
  theme(legend.position = "none")

# topic 2
dat1 <- cplot(m2, x = "racialresentment", dx = "drVignette", what = "effect", data = plotData[plotData[["guilty"]] == 0,], draw = F)
dat2 <- cplot(m2, x = "racialresentment", dx = "drVignette", what = "effect", data = plotData[plotData[["guilty"]] == 1,], draw = F)
dat1$guilty <- "Not Guilty"
dat2$guilty <- "Guilty"
dat <- rbind(dat1, dat2)
ggplot(dat, aes(x = xvals, y=yvals)) + 
  geom_line(aes(color=guilty)) +
  geom_ribbon(aes(ymin = lower, ymax = upper, fill=guilty), alpha = 0.2) +
  geom_hline(yintercept = 0, linetype = 2) +
  ggtitle(myTopics[2]) +
  xlab("Racial Resentment") + ylab("Marginal Effect of Jamal Treatment") +
  scale_fill_manual(values = c("grey8", "grey47")) +
  scale_color_manual(values = c("darkgrey", "darkgrey")) + 
  theme_bw() + 
  theme(legend.position = "none")

# topic 3
dat1 <- cplot(m3, x = "racialresentment", dx = "drVignette", what = "effect", data = plotData[plotData[["guilty"]] == 0,], draw = F)
dat2 <- cplot(m3, x = "racialresentment", dx = "drVignette", what = "effect", data = plotData[plotData[["guilty"]] == 1,], draw = F)
dat1$guilty <- "Not Guilty"
dat2$guilty <- "Guilty"
dat <- rbind(dat1, dat2)
ggplot(dat, aes(x = xvals, y=yvals)) + 
  geom_line(aes(color=guilty)) +
  geom_ribbon(aes(ymin = lower, ymax = upper, fill=guilty), alpha = 0.2) +
  geom_hline(yintercept = 0, linetype = 2) +
  ggtitle(myTopics[3]) +
  xlab("Racial Resentment") + ylab("Marginal Effect of Jamal Treatment") +
  scale_fill_manual(values = c("grey8", "grey47")) +
  scale_color_manual(values = c("darkgrey", "darkgrey")) + 
  theme_bw() + 
  theme(legend.position = "none")

# topic 4
dat1 <- cplot(m4, x = "racialresentment", dx = "drVignette", what = "effect", data = plotData[plotData[["guilty"]] == 0,], draw = F)
dat2 <- cplot(m4, x = "racialresentment", dx = "drVignette", what = "effect", data = plotData[plotData[["guilty"]] == 1,], draw = F)
dat1$guilty <- "Not Guilty"
dat2$guilty <- "Guilty"
dat <- rbind(dat1, dat2)
ggplot(dat, aes(x = xvals, y=yvals)) + 
  geom_line(aes(color=guilty)) +
  geom_ribbon(aes(ymin = lower, ymax = upper, fill=guilty), alpha = 0.2) +
  geom_hline(yintercept = 0, linetype = 2) +
  ggtitle(myTopics[4]) +
  xlab("Racial Resentment") + ylab("Marginal Effect of Jamal Treatment") +
  scale_fill_manual(values = c("grey8", "grey47")) +
  scale_color_manual(values = c("darkgrey", "darkgrey")) + 
  theme_bw() + 
  theme(legend.position = "none")

# topic 5
dat1 <- cplot(m5, x = "racialresentment", dx = "drVignette", what = "effect", data = plotData[plotData[["guilty"]] == 0,], draw = F)
dat2 <- cplot(m5, x = "racialresentment", dx = "drVignette", what = "effect", data = plotData[plotData[["guilty"]] == 1,], draw = F)
dat1$guilty <- "Not Guilty"
dat2$guilty <- "Guilty"
dat <- rbind(dat1, dat2)
ggplot(dat, aes(x = xvals, y=yvals)) + 
  geom_line(aes(color=guilty)) +
  geom_ribbon(aes(ymin = lower, ymax = upper, fill=guilty), alpha = 0.2) +
  geom_hline(yintercept = 0, linetype = 2) +
  ggtitle(myTopics[5]) +
  xlab("Racial Resentment") + ylab("Marginal Effect of Jamal Treatment") +
  scale_fill_manual(values = c("grey8", "grey47")) +
  scale_color_manual(values = c("darkgrey", "darkgrey")) + 
  theme_bw()+ 
  theme(legend.position = "none")

# topic 6
dat1 <- cplot(m6, x = "racialresentment", dx = "drVignette", what = "effect", data = plotData[plotData[["guilty"]] == 0,], draw = F)
dat2 <- cplot(m6, x = "racialresentment", dx = "drVignette", what = "effect", data = plotData[plotData[["guilty"]] == 1,], draw = F)
dat1$guilty <- "Not Guilty"
dat2$guilty <- "Guilty"
dat <- rbind(dat1, dat2)
ggplot(dat, aes(x = xvals, y=yvals)) + 
  geom_line(aes(color=guilty)) +
  geom_ribbon(aes(ymin = lower, ymax = upper, fill=guilty), alpha = 0.2) +
  geom_hline(yintercept = 0, linetype = 2) +
  ggtitle(myTopics[6]) +
  xlab("Racial Resentment") + ylab("Marginal Effect of Jamal Treatment") +
  scale_fill_manual(values = c("grey8", "grey47")) +
  scale_color_manual(values = c("darkgrey", "darkgrey")) + 
  theme_bw()+ 
  theme(legend.position = "none")


## ===============================
## Appendix Figure 7: STM Content 
## ===============================

# create a 4 category variable for content
myData$content <- NA
myData$content[which(myData$guilty == 0 & myData$drVignette == 0) ] <- 0
myData$content[which(myData$guilty == 0 & myData$drVignette == 1) ] <- 1
myData$content[which(myData$guilty == 1 & myData$drVignette == 0) ] <- 2
myData$content[which(myData$guilty == 1 & myData$drVignette == 1) ] <- 3

# get data set up
processed <- textProcessor(myData$UMA406, stem=F, metadata = myData, sparselevel=0.998, customstopwords = c("bradley", "schwartz", "jamal", "gaines"))
out <- prepDocuments(processed$documents, processed$vocab, processed$meta)
docs <- out$documents
vocab <- out$vocab
meta <- out$meta


# fit model
stmFit <- stm(documents = out$documents,
                      vocab = out$vocab, 
                      K = 6, 
                      prevalence =~ drVignette * racialresentment * guilty,
                      content =~ drVignette,
                      max.em.its = 1000, seed=34680349,
                      data = out$meta, 
                      init.type = "Spectral")

selected6ModelResentment <- stmFit

# pull topics
myLabels <- sageLabels(selected6ModelResentment)$marginal
myLabels <- myLabels$frex[,1:5]
myTopics <- apply(myLabels, 1, paste, collapse=", ")

# plot words within topic
pdf("perspectives-contentModel-topic1.pdf", height=5,width=7)
plot(selected6ModelResentment, type = "perspectives", topics = 1, plabels = c("Bradley", "Jamal"))
dev.off()

pdf("perspectives-contentModel-topic2.pdf", height=5,width=7)
plot(selected6ModelResentment, type = "perspectives", topics = 2, plabels = c("Bradley", "Jamal"))
dev.off()

pdf("perspectives-contentModel-topic3.pdf", height=5,width=7)
plot(selected6ModelResentment, type = "perspectives", topics = 3, plabels = c("Bradley", "Jamal"))
dev.off()

pdf("perspectives-contentModel-topic4.pdf", height=5,width=7)
plot(selected6ModelResentment, type = "perspectives", topics = 4, plabels = c("Bradley", "Jamal"))
dev.off()

pdf("perspectives-contentModel-topic5.pdf", height=5,width=7)
plot(selected6ModelResentment, type = "perspectives", topics = 5, plabels = c("Bradley", "Jamal"))
dev.off()

pdf("perspectives-contentModel-topic6.pdf", height=5,width=7)
plot(selected6ModelResentment, type = "perspectives", topics = 6, plabels = c("Bradley", "Jamal"))
dev.off()

# -=-=-=-=-=-=-=-=-=-=
# change plots to feature differences across guilt within treatment
# =-=-=-=-=-=-=-=-=-=-

# M1: predicted values plots
whiteNot <- cplot(m1, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 0 & plotData[["drVignette"]] == 0,], draw = F)
whiteGuilty <- cplot(m1, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 1 & plotData[["drVignette"]] == 0,], draw = F)
blackNot <- cplot(m1, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 0 & plotData[["drVignette"]] == 1,], draw = F)
blackGuilty <- cplot(m1, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 1 & plotData[["drVignette"]] == 1,], draw = F)

whiteNot$Guilty <- "Not Guilty"
whiteGuilty$Guilty <- "Guilty"
blackNot$Guilty <- "Not Guilty"
blackGuilty$Guilty <- "Guilty"

dat1 <- rbind(whiteNot, whiteGuilty)
ggplot(dat1, aes(x = xvals, y=yvals)) + 
  geom_line(aes(color=Guilty)) +
  geom_ribbon(aes(ymin = lower, ymax = upper, fill=Guilty), alpha = 0.2) +
  geom_hline(yintercept = 0, linetype = 2) +
  ggtitle(myTopics[1]) +
  xlab("Racial Resentment") + ylab("Predicted Topic Proportion") +
  scale_fill_manual(values = c("grey47", "grey8")) +
  scale_color_manual(values = c("darkgrey", "darkgrey")) + 
  theme_bw()

dat2 <- rbind(blackNot, blackGuilty)
ggplot(dat2, aes(x = xvals, y=yvals)) + 
  geom_line(aes(color=Guilty)) +
  geom_ribbon(aes(ymin = lower, ymax = upper, fill=Guilty), alpha = 0.2) +
  geom_hline(yintercept = 0, linetype = 2) +
  ggtitle(myTopics[1]) +
  xlab("Racial Resentment") + ylab("Predicted Topic Proportion") +
  scale_fill_manual(values = c("grey47", "grey8")) +
  scale_color_manual(values = c("darkgrey", "darkgrey")) + 
  theme_bw()

# M2: predicted values plots
whiteNot <- cplot(m2, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 0 & plotData[["drVignette"]] == 0,], draw = F)
whiteGuilty <- cplot(m2, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 1 & plotData[["drVignette"]] == 0,], draw = F)
blackNot <- cplot(m2, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 0 & plotData[["drVignette"]] == 1,], draw = F)
blackGuilty <- cplot(m2, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 1 & plotData[["drVignette"]] == 1,], draw = F)

whiteNot$Guilty <- "Not Guilty"
whiteGuilty$Guilty <- "Guilty"
blackNot$Guilty <- "Not Guilty"
blackGuilty$Guilty <- "Guilty"

dat1 <- rbind(whiteNot, whiteGuilty)
ggplot(dat1, aes(x = xvals, y=yvals)) + 
  geom_line(aes(color=Guilty)) +
  geom_ribbon(aes(ymin = lower, ymax = upper, fill=Guilty), alpha = 0.2) +
  geom_hline(yintercept = 0, linetype = 2) +
  ggtitle(myTopics[2]) +
  xlab("Racial Resentment") + ylab("Predicted Topic Proportion") +
  scale_fill_manual(values = c("grey47", "grey8")) +
  scale_color_manual(values = c("darkgrey", "darkgrey")) + 
  theme_bw()

dat2 <- rbind(blackNot, blackGuilty)
ggplot(dat2, aes(x = xvals, y=yvals)) + 
  geom_line(aes(color=Guilty)) +
  geom_ribbon(aes(ymin = lower, ymax = upper, fill=Guilty), alpha = 0.2) +
  geom_hline(yintercept = 0, linetype = 2) +
  ggtitle(myTopics[2]) +
  xlab("Racial Resentment") + ylab("Predicted Topic Proportion") +
  scale_fill_manual(values = c("grey47", "grey8")) +
  scale_color_manual(values = c("darkgrey", "darkgrey")) + 
  theme_bw()

# M3: predicted values plots
whiteNot <- cplot(m3, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 0 & plotData[["drVignette"]] == 0,], draw = F)
whiteGuilty <- cplot(m3, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 1 & plotData[["drVignette"]] == 0,], draw = F)
blackNot <- cplot(m3, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 0 & plotData[["drVignette"]] == 1,], draw = F)
blackGuilty <- cplot(m3, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 1 & plotData[["drVignette"]] == 1,], draw = F)

whiteNot$Guilty <- "Not Guilty"
whiteGuilty$Guilty <- "Guilty"
blackNot$Guilty <- "Not Guilty"
blackGuilty$Guilty <- "Guilty"

dat1 <- rbind(whiteNot, whiteGuilty)
ggplot(dat1, aes(x = xvals, y=yvals)) + 
  geom_line(aes(color=Guilty)) +
  geom_ribbon(aes(ymin = lower, ymax = upper, fill=Guilty), alpha = 0.2) +
  geom_hline(yintercept = 0, linetype = 2) +
  ggtitle(myTopics[3]) +
  xlab("Racial Resentment") + ylab("Predicted Topic Proportion") +
  scale_fill_manual(values = c("grey47", "grey8")) +
  scale_color_manual(values = c("darkgrey", "darkgrey")) + 
  theme_bw()

dat2 <- rbind(blackNot, blackGuilty)
ggplot(dat2, aes(x = xvals, y=yvals)) + 
  geom_line(aes(color=Guilty)) +
  geom_ribbon(aes(ymin = lower, ymax = upper, fill=Guilty), alpha = 0.2) +
  geom_hline(yintercept = 0, linetype = 2) +
  ggtitle(myTopics[3]) +
  xlab("Racial Resentment") + ylab("Predicted Topic Proportion") +
  scale_fill_manual(values = c("grey47", "grey8")) +
  scale_color_manual(values = c("darkgrey", "darkgrey")) + 
  theme_bw()

# M4: predicted values plots
whiteNot <- cplot(m4, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 0 & plotData[["drVignette"]] == 0,], draw = F)
whiteGuilty <- cplot(m4, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 1 & plotData[["drVignette"]] == 0,], draw = F)
blackNot <- cplot(m4, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 0 & plotData[["drVignette"]] == 1,], draw = F)
blackGuilty <- cplot(m4, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 1 & plotData[["drVignette"]] == 1,], draw = F)

whiteNot$Guilty <- "Not Guilty"
whiteGuilty$Guilty <- "Guilty"
blackNot$Guilty <- "Not Guilty"
blackGuilty$Guilty <- "Guilty"

dat1 <- rbind(whiteNot, whiteGuilty)
ggplot(dat1, aes(x = xvals, y=yvals)) + 
  geom_line(aes(color=Guilty)) +
  geom_ribbon(aes(ymin = lower, ymax = upper, fill=Guilty), alpha = 0.2) +
  geom_hline(yintercept = 0, linetype = 2) +
  ggtitle(myTopics[4]) +
  xlab("Racial Resentment") + ylab("Predicted Topic Proportion") +
  scale_fill_manual(values = c("grey47", "grey8")) +
  scale_color_manual(values = c("darkgrey", "darkgrey")) + 
  theme_bw()

dat2 <- rbind(blackNot, blackGuilty)
ggplot(dat2, aes(x = xvals, y=yvals)) + 
  geom_line(aes(color=Guilty)) +
  geom_ribbon(aes(ymin = lower, ymax = upper, fill=Guilty), alpha = 0.2) +
  geom_hline(yintercept = 0, linetype = 2) +
  ggtitle(myTopics[4]) +
  xlab("Racial Resentment") + ylab("Predicted Topic Proportion") +
  scale_fill_manual(values = c("grey47", "grey8")) +
  scale_color_manual(values = c("darkgrey", "darkgrey")) + 
  theme_bw()


# M5: predicted values plots
whiteNot <- cplot(m5, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 0 & plotData[["drVignette"]] == 0,], draw = F)
whiteGuilty <- cplot(m5, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 1 & plotData[["drVignette"]] == 0,], draw = F)
blackNot <- cplot(m5, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 0 & plotData[["drVignette"]] == 1,], draw = F)
blackGuilty <- cplot(m5, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 1 & plotData[["drVignette"]] == 1,], draw = F)

whiteNot$Guilty <- "Not Guilty"
whiteGuilty$Guilty <- "Guilty"
blackNot$Guilty <- "Not Guilty"
blackGuilty$Guilty <- "Guilty"

dat1 <- rbind(whiteNot, whiteGuilty)
ggplot(dat1, aes(x = xvals, y=yvals)) + 
  geom_line(aes(color=Guilty)) +
  geom_ribbon(aes(ymin = lower, ymax = upper, fill=Guilty), alpha = 0.2) +
  geom_hline(yintercept = 0, linetype = 2) +
  ggtitle(myTopics[5]) +
  xlab("Racial Resentment") + ylab("Predicted Topic Proportion") +
  scale_fill_manual(values = c("grey47", "grey8")) +
  scale_color_manual(values = c("darkgrey", "darkgrey")) + 
  theme_bw()

dat2 <- rbind(blackNot, blackGuilty)
ggplot(dat2, aes(x = xvals, y=yvals)) + 
  geom_line(aes(color=Guilty)) +
  geom_ribbon(aes(ymin = lower, ymax = upper, fill=Guilty), alpha = 0.2) +
  geom_hline(yintercept = 0, linetype = 2) +
  ggtitle(myTopics[5]) +
  xlab("Racial Resentment") + ylab("Predicted Topic Proportion") +
  scale_fill_manual(values = c("grey47", "grey8")) +
  scale_color_manual(values = c("darkgrey", "darkgrey")) + 
  theme_bw()


# M6: predicted values plots
whiteNot <- cplot(m6, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 0 & plotData[["drVignette"]] == 0,], draw = F)
whiteGuilty <- cplot(m6, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 1 & plotData[["drVignette"]] == 0,], draw = F)
blackNot <- cplot(m6, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 0 & plotData[["drVignette"]] == 1,], draw = F)
blackGuilty <- cplot(m6, x = "racialresentment", what = "prediction", data = plotData[plotData[["guilty"]] == 1 & plotData[["drVignette"]] == 1,], draw = F)

whiteNot$Guilty <- "Not Guilty"
whiteGuilty$Guilty <- "Guilty"
blackNot$Guilty <- "Not Guilty"
blackGuilty$Guilty <- "Guilty"

dat1 <- rbind(whiteNot, whiteGuilty)
ggplot(dat1, aes(x = xvals, y=yvals)) + 
  geom_line(aes(color=Guilty)) +
  geom_ribbon(aes(ymin = lower, ymax = upper, fill=Guilty), alpha = 0.2) +
  geom_hline(yintercept = 0, linetype = 2) +
  ggtitle(myTopics[6]) +
  xlab("Racial Resentment") + ylab("Predicted Topic Proportion") +
  scale_fill_manual(values = c("grey47", "grey8")) +
  scale_color_manual(values = c("darkgrey", "darkgrey")) + 
  theme_bw()

dat2 <- rbind(blackNot, blackGuilty)
ggplot(dat2, aes(x = xvals, y=yvals)) + 
  geom_line(aes(color=Guilty)) +
  geom_ribbon(aes(ymin = lower, ymax = upper, fill=Guilty), alpha = 0.2) +
  geom_hline(yintercept = 0, linetype = 2) +
  ggtitle(myTopics[6]) +
  xlab("Racial Resentment") + ylab("Predicted Topic Proportion") +
  scale_fill_manual(values = c("grey47", "grey8")) +
  scale_color_manual(values = c("darkgrey", "darkgrey")) + 
  theme_bw()

# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Appendix Section 11: Party & Ideology
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

myData$vignetteFactor <- as.factor(myData$drVignette)
logitBasic <- glm(guilty ~ vignetteFactor, data=myData, family=binomial(link="logit"))
logitInt <- glm(guilty ~ vignetteFactor * racialresentment + vignetteFactor * party, data=myData, family=binomial(link="logit"))

plotLogit <- ggpredict(logitInt, terms = c("racialresentment", "vignetteFactor", "party"))
p1 <- plot(plotLogit, colors = c("grey76", "grey32"), alpha = .5)
p1 <- p1 + labs(x="Racial Resentment", y = "Predicted Probability of Guilty", title = "Predicted Probabilities") +
  theme(legend.position="none", legend.title=element_blank()) +
  scale_fill_manual(breaks = c(0,1), values=c("grey76", "grey32"), name="", labels=c("White", "Black")) + 
  scale_color_manual(breaks = c(0,1), values=c("grey76", "grey32"), name="", labels=c("White", "Black")) 

myData$party <- 2
myData$party[grep("Republican", myData$pid7)] <- 3
myData$party[grep("Democrat", myData$pid7)] <- 1

mpartylog <- glm(guilty ~ vignetteFactor * racialresentment + vignetteFactor * party, data=plotData, family=binomial(link = "logit"))
mideolog <- glm(guilty ~ vignetteFactor * racialresentment + vignetteFactor * ideology, data=plotData, family=binomial(link = "logit"))

plotLogit <- ggpredict(mpartylog, terms = c("racialresentment", "vignetteFactor", "party"))
p1 <- plot(plotLogit, colors = c("grey76", "grey32"), alpha = .5)
p1 <- p1 + labs(x="Racial Resentment", y = "Predicted Probability of Guilty", title = "Predicted Probabilities") +
  theme(legend.position="none", legend.title=element_blank()) +
  scale_fill_manual(breaks = c(0,1), values=c("grey76", "grey32"), name="", labels=c("White", "Black")) + 
  scale_color_manual(breaks = c(0,1), values=c("grey76", "grey32"), name="", labels=c("White", "Black")) 

plotLogit <- ggpredict(mideolog, terms = c("racialresentment", "vignetteFactor", "ideology"))
p2 <- plot(plotLogit, colors = c("grey76", "grey32"), alpha = .5)
p2 <- p2 + labs(x="Racial Resentment", y = "Predicted Probability of Guilty", title = "Predicted Probabilities") +
  theme(legend.position="none", legend.title=element_blank()) +
  scale_fill_manual(breaks = c(0,1), values=c("grey76", "grey32"), name="", labels=c("White", "Black")) + 
  scale_color_manual(breaks = c(0,1), values=c("grey76", "grey32"), name="", labels=c("White", "Black")) 
